package com.CGFrame.struts.bean.jobSchedule;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;

import com.CGFrame.struts.POJOs.Category;
import com.CGFrame.struts.POJOs.Make;
import com.CGFrame.struts.POJOs.Element;
import com.CGFrame.struts.POJOs.Elementgroup;
import com.CGFrame.struts.POJOs.Shot;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.CGFrame.struts.bean.unit.ElementGroupBean;
import com.CGFrame.struts.bean.unit.UnitBean;

public class ESchedule {
	private Elementgroup elementGroup;
	private ProjectOperate pop;
	public ESchedule(String elementGroupId) throws Exception 
	{
		pop = ProjectOperateFactory.getprojectOperate();
		elementGroup = (Elementgroup)pop.UniqueQuery("Elementgroup", "elementGroupId,"+elementGroupId );
		if (elementGroup == null)
		{
			throw new Exception("No The ElementGroup");
		}
		
	}
	public StringBuffer getEGHeader()
	{
		StringBuffer result= new StringBuffer("ID$ElementName$Short$StartTime$EndTime");
		
		List elementCategories = pop.Query("Category", "company.companyId,"+elementGroup.getProject().getCompany().getCompanyId()+",categoryType,Element", "categoryId", "asc");
		int size = elementCategories.size();
		for (int i = 0;i<size;i++)
		{
			Category category = (Category)elementCategories.get(i);
			result.append("$"+category.getCategoryName()+"");
		}
		return result;
	}
	
	
	public LinkedHashSet getElementSchedule()
	{
		LinkedHashSet elementSchedule = new LinkedHashSet();
		ElementGroupBean eGBean = new ElementGroupBean(elementGroup);
		Iterator ite_elements = eGBean.getAllElements().iterator();
		while(ite_elements.hasNext())
		{
			Element element = (Element)ite_elements.next();
			elementSchedule.add(getDateByElement(element));
		}
		return elementSchedule;
	}
	
	public static StringBuffer getDateByElement(Element element)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		StringBuffer result = new StringBuffer(""+element.getElementId()+"$"+element.getElementName()+"$"+element.getShortening()+"");
		String startTime = "";
		String endTime   = "";
		try
		{
			SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
			startTime = dateFm.format(element.getPlanStartTime());
			endTime   = dateFm.format(element.getPlanEndTime());
		}
		catch (Exception e)
		{
			// Time is  null 
		}
		result.append("$").append(startTime).append("$").append(endTime);
		
		UnitBean unitBean = new UnitBean("Element",element.getElementId());
		List elementCategories = pop.Query("Category", "company.companyId,"+element.getElementgroup().getProject().getCompany().getCompanyId()+",categoryType,Element", "categoryId", "asc");
		int size = elementCategories.size();
		for (int i = 0 ; i<size ; i++)
		{
			Category category = (Category)elementCategories.get(i);
			Make make;
			if ( ( make =unitBean.getMakeByCategory(category) )==null )
			{
				result.append("$ ");
				
			}
			else
			{
				result.append("$"+make.getStatus().getStatusName()+"" );
			}
		}
		return result;
	}
}
